<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\model\forest\Member;
use app\common\model\Gift;
use app\common\model\MoneyLog;
use app\common\model\Order;
use app\common\model\ScoreLog;
use app\common\model\User;
use think\Db;

/**
 * 定时任务接口
 */
class Crontab extends Api
{

    //如果$noNeedLogin为空表示所有接口都需要登录才能请求
    //如果$noNeedRight为空表示所有接口都需要验证权限才能请求
    //如果接口已经设置无需登录,那也就无需鉴权了
    //
    // 无需登录的接口,*表示全部
    protected $noNeedLogin = ['*'];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];

    /**
     * 季度收益
     *
     */
    public function quarterly_earnings()
    {
        $month = date('m');
        $day = date('d');
        if (in_array($month, ['3', '6', '9']) && $day == '30') {
            $user = User::all(['forest_level' => ['>', 0], 'validitytime' => ['>', time()]]);
            foreach ($user as &$v) {
                $check = MoneyLog::whereTime('createtime', 'today')->where('source_type', 502)->count();
                if (!$check) {
                    $ids = User::where("find_in_set({$v->id},paren_ids)")->column('id');
                    $score = ScoreLog::whereTime('createtime', 'today')->where('type', 2)->whereIn('user_id', array_push($ids, $v->id))->sum('score');
                    if ($score < $v->money) {
                        continue;
                    } else {
                        $member = Member::get($v->forest_level);
                        $money = bcmul($member->price, 0.1, 2);
                        MoneyLog::income($v->id, $money, 2, 502);
                    }
                }
            }
        }
    }
    /**
     * 过期森林会员处理
     *
     */
    // public function expire_member()
    // {
    //     $user = User::all(['forest_level' => ['>', 0], 'validitytime' => ['<', time()]]);
    //     foreach ($user as &$v) {
    //         $v->save(['forest_level' => 0]);
    //     }
    // }

    //团队奖金
    public function team_bonus()
    {
        Db::startTrans();
        try {
            $count = MoneyLog::whereTime('createtime', 'yesterday')->where('source_type', 503)->count();
            if (!$count) {
                $total_fee = Order::where('status', '>', 0)->whereTime('paytime', 'yesterday')->sum('pay_money-total_original');
                $total_leaf = User::sum('gold_leaf');
                $unit_fee = $total_fee / $total_leaf;
                $users = User::all(['gold_leaf' => ['>', 0]]);
                foreach ($users as $k => $v) {
                    MoneyLog::income($v->id, bcmul($unit_fee, $v['gold_leaf']), 0, 503);
                }
            }
            Db::commit();
        } catch (\Throwable $th) {
            Db::rollback();
            return $th->getMessage();
        }
    }

    //叶子会员每月到账GR
    public function leaf_month_gr()
    {
        Db::startTrans();
        try {
            $users = User::where('fee', '>', 0)->where('month', '>', 0)->select();
            $gr_rate = config('site.gr_rate') ?? 0;
            $no_gr_rate = 100 - $gr_rate;
            foreach ($users as $k => $v) {
                $month_fee = $v['fee'] / $v['month'];
                $gr = 0;
                $no_gr = 0;
                if ($gr_rate > 0) {
                    $gr = bcmul($month_fee, $gr_rate / 100, 2);
                }
                if ($no_gr_rate > 0) {
                    $no_gr = bcmul($month_fee, $no_gr_rate / 100, 2);
                }
                $data = [
                    'user_id' => $v['id'],
                    'gr' => $gr,
                    'no_gr' => $no_gr,
                    'price' => $month_fee,
                    'createdate' => date('Ym'),
                    'money' => bcsub($v['fee'], $month_fee, 2),
                ];
                $res = Gift::create($data);
                if ($res) {
                    MoneyLog::income($v['id'], $gr, 2, 201, '', 0);
                    MoneyLog::income($v['id'], $no_gr, 2, 202, '', 1);
                    $v->setDec('fee', $month_fee);
                    $v->setDec('month');
                }
            }
            Db::commit();
        } catch (\Throwable $th) {
            Db::rollback();
            return $th->getMessage();
        }
    }
}
